package com.hugoo.core.db.pools;

import com.hugoo.core.Conf;
import com.hugoo.core.util.TL;
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public final class LxOraclePool implements NiCP {

    private final static Log log = LogFactory.getLog(LxOraclePool.class);
    MCPM mcpm = null;
    private final String inst;

    public LxOraclePool(String inst) {
        this.inst = inst;
        init();
    }

    @Override
    public void init() {
        try {
            OracleConnectionPoolDataSource dataSource = new OracleConnectionPoolDataSource();

            log.info("protocol:" + Conf.getBaseProp(inst, "database.protocol") + "\n"
                    + "server:" + Conf.getBaseProp(inst, "database.server") + "\n"
                    + "port:" + Conf.getBaseProp(inst, "database.port") + "\n"
                    + "sid:" + Conf.getBaseProp(inst, "database.sid") + "\n"
                    + "userName:" + Conf.getBaseProp(inst, "database.userName"));
            dataSource.setDriverType(Conf.getBaseProp(inst, "database.protocol"));
            dataSource.setServerName(Conf.getBaseProp(inst, "database.server"));
            dataSource.setPortNumber(TL.parseInt(Conf.getBaseProp(inst, "database.port"), 1521));
            dataSource.setServiceName(Conf.getBaseProp(inst, "database.sid"));
            dataSource.setUser(Conf.getBaseProp(inst, "database.userName"));
            dataSource.setPassword(Conf.getBaseProp(inst, "database.password"));
            mcpm = new MCPM(dataSource, 30);//设置最大并发连接数为30
        } catch (SQLException ex) {
            log.error("初始化数据库连接异常！", ex);
        }
    }

    @Override
    public Connection getConn() {
        try {
            return mcpm.getConnection();
        } catch (SQLException ex) {
            log.error("打开数据库连接失败！", ex);
        } catch (Exception ex) {
            log.error("打开数据库连接出现异常！", ex);
        }
        return null;
    }
}
